package com.py.payment.tools.date;

import com.py.payment.tools.ToolsUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String HH_MM = "HH:mm";


    /**
     * 获取今天开始时间
     * @return
     */
    public static Date zeroTime(){
       try{
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
           SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           Date nowTime = new Date();
           String zero = sdf.format(nowTime)+" 00:00:00";
           Date zeroTime = sdf1.parse(zero);
           return zeroTime;
       }catch (Exception e){
           e.printStackTrace();
           return null;
       }
    }

    /**
     * 获取今天结束时间
     * @return
     */
    public static Date endTime(){
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date nowTime = new Date();
            String end = sdf.format(nowTime)+" 23:59:59";
            Date endTime = sdf1.parse(end);
            return endTime;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 毫秒转变为时间格式字符串
     * @param time
     * @return
     */
    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0) {
            return "00:00";
        } else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99) {
                    return "99:59:59";
                }
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10) {
            retStr = "0" + Integer.toString(i);
        }else {
            retStr = "" + i;
        }
        return retStr;
    }

    /**
     * 现在到今天结束的毫秒数
     */
    public static Long getCurrent2TodayEndMillisTime() {
        Calendar todayEnd = Calendar.getInstance();
        // Calendar.HOUR 12小时制
        // HOUR_OF_DAY 24小时制
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTimeInMillis()-System.currentTimeMillis();
    }

    /**
     * 两个时间相差的小时数
     * @param startDate
     * @param endDate
     * @return
     */
    public static long diffHours(Date startDate, Date endDate) {
        return (endDate.getTime() - startDate.getTime()) / 3600000;
    }

    /**
     * String转Date
     * @param date
     * @param pattern
     * @return
     */
    public static Date datestrToDate(String date, String pattern){
        if(ToolsUtils.isEmpty(pattern)){
            pattern = DateUtils.YYYY_MM_DD_HH_MM_SS;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        if(ToolsUtils.isEmpty(date)){
            return null;
        }

        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    public static String dateToDateStr(Date date, String pattern){
        if(ToolsUtils.isEmpty(pattern)){
            pattern = DateUtils.YYYY_MM_DD_HH_MM_SS;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        if(ToolsUtils.isEmpty(date)){
            return null;
        }

        return sdf.format(date);
    }
}
